﻿using System;
using System.Collections.Generic;
using System.Text;

using SugarWebApi.Commands;

namespace SugarWebApi
{
	public abstract class SugarWebProcessor
	{

		#region Constructors

		public SugarWebProcessor(
			string url)
		{
			this._url = url;
		}

		#endregion

		#region Fields

		private string _url;

		#endregion

		#region Properties

		internal string Url
		{
			get { return this._url; }
			set
			{
				if (this.Url == value) return;
				this._url = value;
				this.OnUrlChanged(new EventArgs());
			}
		}

		#endregion

		#region Methods

		protected virtual void OnUrlChanged(EventArgs e)
		{
			// nothing to do 
		}

		public SugarWebResult Process(
			SugarWebCommand command)
		{
			return this.ProcesInternal(command);
		}

		protected virtual SugarWebResult ProcesInternal(
			SugarWebCommand command)
		{
			if (command is LoginCommand) return ProcesLoginCommand(command as LoginCommand);
			else if (command is GetAvailableModulesCommand) return ProcesGetAvailableModeulsCommand(command as GetAvailableModulesCommand);
			else if (command is LogoutCommand) return ProcesLogoutCommand(command as LogoutCommand);
			else if (command is GetEntryListCommand) return ProcesGetEntryListCommand(command as GetEntryListCommand);
			else if (command is GetModuleFieldsCommand) return ProcesGetModuleFieldsCommand(command as GetModuleFieldsCommand);
			else if (command is SetEntryCommand) return ProcesSetEntryCommand(command as SetEntryCommand);
			else throw new NotImplementedException();
		}

		protected abstract GetEntryListResult ProcesGetEntryListCommand(
			GetEntryListCommand command);

		protected abstract GetAvailableModulesResult ProcesGetAvailableModeulsCommand(
			GetAvailableModulesCommand command);

		protected abstract LoginResult ProcesLoginCommand(
			LoginCommand command);

		protected abstract LogoutResult ProcesLogoutCommand(
			LogoutCommand command);

		protected abstract GetModuleFieldsResult ProcesGetModuleFieldsCommand(
			GetModuleFieldsCommand command);

		protected abstract SetEntryResult ProcesSetEntryCommand(
			SetEntryCommand command);

		#endregion

	}
}
